//
//  HomeViewController.m
//  Text2Voice
//
//  Created by Cluy on 14-11-3.
//  Copyright (c) 2014年 sunflows. All rights reserved.
//

#import "HomeViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
  
    synthesizer = [[AVSpeechSynthesizer alloc]init];
    [synthesizer setDelegate:self];
    
//    synthVoice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh_CN"];
//    synthVoice = [AVSpeechSynthesisVoice voiceWithLanguage:[AVSpeechSynthesisVoice currentLanguageCode]];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)Text2Voice:(id)sender {
    utterace = [[AVSpeechUtterance alloc]initWithString:notes.text];
//    utterace.voice = synthVoice;
    [utterace setRate:AVSpeechUtteranceMaximumSpeechRate];
    [synthesizer speakUtterance:utterace];
}
- (IBAction)GText2Voice:(id)sender {
    NSString *Notes = notes.text;
    NSString *urlString = [NSString stringWithFormat:@"http://translate.google.com/translate_tts?ie=UTF-8&oe=UTF-8&tl=zh&q=%@",Notes];

    NSString *url = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData *mp3Data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];

    //播放
    AVAudioPlayer *translatePlayer = [[AVAudioPlayer alloc] initWithData:mp3Data error:nil];
    [translatePlayer play];
}
@end
